import gugga.events.EventDispatcher;
import gugga.common.ITask;
import gugga.utils.DoLaterUtil;

/**
 * @author Todor Kolev
 */
class gugga.sequence.FrameWaitingTask extends EventDispatcher implements ITask 
{
	private var mIsRunning : Boolean = false;
	private var mFramesDelay : Number;
	
	function FrameWaitingTask(aFramesDelay:Number) 
	{
		super();
		mFramesDelay = aFramesDelay;
	}

	public function start() : Void 
	{
		mIsRunning = true;
		dispatchEvent({type: "start", target: this});
		
		if(mFramesDelay > 0)
		{
			DoLaterUtil.doLater(this, onCompleted, null, mFramesDelay);
		}
		else
		{
			onCompleted();
		}
	}
	
	private function onCompleted() : Void
	{ 
		if(mIsRunning)
		{
			mIsRunning = false;
			dispatchEvent({type: "completed", target: this});
		}
	}

	public function isRunning() : Boolean 
	{
		return mIsRunning;
	}

	public function isImmediatelyInterruptable() : Boolean 
	{
		return true;
	}

	public function interrupt() : Void 
	{
		mIsRunning = true;
		dispatchEvent({type: "interrupted", target: this});
	}
}